layui.use(['engine_table', 'http', 'public'], function () {
    current = layui.public.GetQuery('current') || 1;
    onLoad();
    LocalFile();
})

function onLoad() {
    layui.engine_table.TableLoadBack();
}

function SearchLoad(elem = '', type = '') {
    layui.engine_table.SearchLoad(elem, type);
}

function tab(obj) {
    $('.button_group button').removeClass("btn_theme_3");
    $(obj).addClass("btn_theme_3");
    current = $(obj).attr("data-current") || 1;
    SettingData['datatable']['table']['url'] = table_config['table']['url'];
    SettingData['datatable']['table']['token'] =localStorage.getItem('user_token') || 'no';
    if (current == 4) {
        SettingData['datatable']['table']['url'] = '/common/shop/addon/';
        SettingData['datatable']['table']['token'] ='';
    }

    layui.engine_table.SearchLoad();
}

function UserInfo() {
    layui.public.OpenWindow('/common/shop/userinfo', '会员信息', '500', '500')
}



function LocalFile() {

    let token = localStorage.getItem('token') || '';
    layui.upload.render({
        elem: "#local_Install",
        url: '/common/shop/localfile/',
        accept: 'file',
        number: 1,
        exts: 'zip',
        headers: {token: token},
        before: function (res) {
            layer.load();
        },
        done: function (res) {
            layui.layer.closeAll('loading');
            if (res.code == 200) {
                layui.layer.msg('上传成功，请切换本地菜单安装', {icon: 1});
            } else {
                layui.layer.msg(res.msg, {icon: 2});
            }
        },
        error: function (res) {
            layui.layer.msg('请求出错，请稍后再试', {icon: 2});
        }
    })
}

function getDetail(id = 0, title = '') {
    if (id) {
        layui.public.OpenWindow('/common/shop/detail/?id=' + id, '应用详情');
    }
}

function Install(addon = '') {

    layer.confirm('确定要安裝吗？', function (index) {
        layer.close(index);
        layui.http.SendAjax('/common/shop/install/', {addon: addon}, {}, function (res) {
            if (res.code == 200) {
                layui.layer.msg('安裝成功', {icon: 1});
                layui.engine_table.SearchLoad();
            } else {
                layui.layer.msg(res.msg, {icon: 2});
            }
        })
    })


}

function Uninstall(addon = '') {

    layer.confirm('确定要卸载吗？', function (index) {
        layer.close(index);
        layui.http.SendAjax('/common/shop/uninstall/', {addon: addon}, {}, function (res) {
            if (res.code == 200) {
                layui.layer.msg('卸载成功', {icon: 1});
                layui.engine_table.SearchLoad();
            } else {
                layui.layer.msg(res.msg, {icon: 2});
            }
        })
    })

}



